package com.demo.java.nashorn;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.script.*;
import java.io.FileReader;
import java.io.IOException;

@Slf4j
public class Nashorntest {
    static ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");

    public static void main(String[] args) throws IOException, ScriptException {
        //bindingsTest();

        Bindings bindings = scriptEngine.createBindings();
        bindings.put("ss", "{\\\"asline\\\":\\\"device1_device1_45,1,8$device1_device1_67,2,5\\\"}");
        scriptEngine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
        Object obj = scriptEngine.eval("var reg = /\\\\/g;JSON.parse(ss.replace(reg,''))");
        System.out.println(obj);


    }

    private static void bindingsTest() throws ScriptException, IOException {
        String batchDataString = "{\"schema\":[{\"dataType\":\"LONG\",\"name\":\"cnt\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"INT\",\"name\":\"hour\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"SHORT\",\"name\":\"status\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"STRING\",\"name\":\"thingname\",\"order\":0,\"extraInfo\":{}}],\"data\":[{\"hour\":10,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":762,\"status\":3},{\"hour\":10,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":679,\"status\":2},{\"hour\":10,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":696,\"status\":4},{\"hour\":10,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":689,\"status\":0},{\"hour\":10,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":751,\"status\":1},{\"hour\":0,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":693,\"status\":1},{\"hour\":0,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":748,\"status\":2},{\"hour\":0,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":719,\"status\":4},{\"hour\":0,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":710,\"status\":3},{\"hour\":0,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":706,\"status\":0},{\"hour\":1,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":722,\"status\":2},{\"hour\":1,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":679,\"status\":3},{\"hour\":1,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":708,\"status\":1},{\"hour\":1,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":756,\"status\":4},{\"hour\":1,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":689,\"status\":0},{\"hour\":2,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":738,\"status\":2},{\"hour\":2,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":717,\"status\":1},{\"hour\":2,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":722,\"status\":0},{\"hour\":2,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":672,\"status\":3},{\"hour\":2,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":747,\"status\":4},{\"hour\":3,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":690,\"status\":1},{\"hour\":3,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":705,\"status\":0},{\"hour\":3,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":695,\"status\":4},{\"hour\":3,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":755,\"status\":2},{\"hour\":3,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":726,\"status\":3},{\"hour\":4,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":702,\"status\":2},{\"hour\":4,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":751,\"status\":1},{\"hour\":4,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":675,\"status\":0},{\"hour\":4,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":750,\"status\":3},{\"hour\":4,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":674,\"status\":4},{\"hour\":5,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":698,\"status\":2},{\"hour\":5,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":715,\"status\":3},{\"hour\":5,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":727,\"status\":4},{\"hour\":5,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":706,\"status\":1},{\"hour\":5,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":750,\"status\":0},{\"hour\":6,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":707,\"status\":0},{\"hour\":6,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":667,\"status\":4},{\"hour\":6,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":698,\"status\":3},{\"hour\":6,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":700,\"status\":2},{\"hour\":6,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":701,\"status\":1},{\"hour\":7,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":694,\"status\":0},{\"hour\":7,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":719,\"status\":3},{\"hour\":7,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":726,\"status\":4},{\"hour\":7,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":701,\"status\":2},{\"hour\":7,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":716,\"status\":1},{\"hour\":8,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":705,\"status\":2},{\"hour\":8,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":720,\"status\":4},{\"hour\":8,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":742,\"status\":0},{\"hour\":8,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":716,\"status\":3},{\"hour\":8,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":673,\"status\":1},{\"hour\":9,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":743,\"status\":2},{\"hour\":9,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":724,\"status\":0},{\"hour\":9,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":693,\"status\":3},{\"hour\":9,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":705,\"status\":1},{\"hour\":9,\"thingname\":\"DeviceStatus_DeviceStatus\",\"cnt\":730,\"status\":4}]}";
        String streamDataString = "{\"schema\":[{\"dataType\":\"TIMESTAMP\",\"name\":\"timestamp\",\"description\":\"Timestamp\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"STRING\",\"name\":\"thingName\",\"description\":\"设备名称\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"TIMESTAMP\",\"name\":\"win_time\",\"description\":\"窗口时间\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"INT\",\"name\":\"hour\",\"description\":\"所属小时\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"INT\",\"name\":\"min\",\"description\":\"分钟窗口\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"BYTE\",\"name\":\"status\",\"description\":\"设备状态\",\"order\":0,\"extraInfo\":{}},{\"dataType\":\"LONG\",\"name\":\"cnt\",\"description\":\"上报数量\",\"order\":0,\"extraInfo\":{}}],\"data\":[]}";

        Object batchData = JSON.parse(batchDataString);
        Object streamData = JSON.parse(streamDataString);

        final ClassPathResource resource = new ClassPathResource("js/nashornTest.js");

        Bindings bindings = scriptEngine.createBindings();
        bindings.put("batchData", batchData);
        bindings.put("streamData", streamData);
        scriptEngine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);

        scriptEngine.eval(new FileReader(resource.getFile()));
    }

    public static void scriptRunTest() throws Exception {
        ClassPathResource resource = new ClassPathResource("js/DeviceStatusBatchAndStreamAgg.js");

//        Bindings bindings = scriptEngine.createBindings();
//        bindings.put("console", log);
//        scriptEngine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);

        scriptEngine.eval(new FileReader(resource.getFile()));
    }
}
